import re
from werkzeug.utils import secure_filename
from info.return_code import *
from info.config import UploadImageConfig
from info.utils.encript import md5

error = ERROR
existed = EXISTED
sucess = SUCCESS
failure = SERVER_ERROR

def v_imageid(imageid):
    return FORBIDDEN if not imageid or not re.match('^[1-9][0-9]{9}$',imageid) else None

def v_tel(tel):
    # 1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}
    if not tel:
        return FORBIDDEN
    if not re.match('^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$',tel):
        return WRONG_CONTENT
    return None

def v_image_code(image_code,session_image_code):
    if not image_code:
        return FORBIDDEN
    if not session_image_code:
        return EXPIRE
    if image_code != session_image_code:
        return WRONG_CONTENT
    return None

def v_sms(sms_ret):
    if sms_ret != 0:
        return SERVER_ERROR
    else:
        return None

def v_tel_code(tel_code,session_tel_code):
    if not tel_code:
        return FORBIDDEN
    if not session_tel_code:
        return EXPIRE
    if tel_code != session_tel_code:
        return WRONG_CONTENT
    return None


def v_password(password):
    if not password:
        return FORBIDDEN
    if not re.match('^[0-9a-zA-Z!@#$%^&*()_]{6,16}$',password):
        return WRONG_FORMAT
    return None

# def v_filename(filename):
#     suffix_name = filename[filename.rfind('.'):]
#     if suffix_name not in UploadImageConfig.ALLOWED_EXTENSIONS:return WRONG_TYPE
#     return None
#
# def v_filesize(filesize):
#     if filesize > UploadImageConfig.ALLOWED_SIZE:return TOO_LARGE
#     return None
def v_file(file):

    filesize = len(file.read())
    if filesize > UploadImageConfig.ALLOWED_SIZE: return TOO_LARGE

    filename = secure_filename(file.filename)
    # print(filename)
    suffix_name = filename[filename.rfind('.'):]
    # print(suffix_name)
    if suffix_name not in UploadImageConfig.ALLOWED_EXTENSIONS: return WRONG_TYPE

    return None



def v_new_old_password(user_password,new_password,old_password,re_password):
    if new_password and old_password and re_password and user_password:
        if md5(old_password) != user_password:
            return WRONG_CONTENT
        if new_password != re_password:
            return WRONG_CONTENT
        return None
    else:
        return WRONG_CONTENT